<!--
 * @Author: mhb
 * @Date: 2022-05-09 16:32:51
 * @LastEditTime: 2023-05-11 10:29:09
 * @LastEditors: Please set LastEditors
 * @Description: 关联Bim模型
 * @FilePath: \cscec-pms-admin\src\components\Dialog\BimRelease.vue
-->
<template>
  <div>
    <a-modal title="关联BIM模型" :visible="visible" v-bind="$attrs" width="90%" @cancel="$emit('update:visible', false)">
      <a-spin :spinning="loading">
        <div class="dialogBox">
          <model-detail :showAllNode="false" :clickable="isRelease"
                        :showBack="false" :checkableDomains="false" :checkableFloor="isRelease"
                        :defaultSelectedFloors="selectedFloors" :key="modelList[0] ? modelList[0].modelId : 0"
                        @modelParameterLoaded="modelParameterLoaded"
                        ref="ModelDetail" :model-list="modelList" :highLightList="highLightList" />
        </div>
      </a-spin>
      <!-- 页脚操作栏 -->
      <div slot="footer" class="btn_box">
        <a-button @click="$emit('update:visible', false)"> 关闭 </a-button>
        <a-button v-if="isRelease && !isConstructData" class="ml12" type="primary" @click="handleOk"  :disabled="!isModelParameterLoaded"> 确认关联 </a-button>
        <a-button v-if="isConstructData" class="ml12" type="primary" @click="confirmSelect" :loading="loading"
        :disabled="!isModelParameterLoaded"
        >确认关联</a-button>
      </div>
    </a-modal>
  </div>
</template>

<script>
import ModelDetail from '@/components/BimIntegrate/index.vue'
import { getBimAssociateFloor } from '@/api/bimIntegrate/bimModel' // api

export default {
  name: 'BimRelease',
  components: {
    ModelDetail
  },
  props: {
    // 显隐
    visible: {
      type: Boolean,
      required: true
    },
    // 模型列表
    modelList: {
      type: Array,
      required: true
    },
    // 是否显示关联按钮
    isRelease: {
      type: Boolean,
      required: true
    },
     // 是否显示关联构建按钮
     isConstructData: {
      type: Boolean,
      required: false
    },
    highLightList: {
      // 需要高亮的实体列表
      type: String,
      default: ''
    },
    
  },
  data() {
    return {
      isModelParameterLoaded:false,
      loading: false, // 关联加载状态
      selectedFloors: [] // 选中的楼层
    }
  },
  watch: {
    visible(val) {
      // 未关联时(查看状态下)，获取模型关联楼层
      if (val) {
        this.$nextTick(() => {
          this.getReleaseFloor()
        })
      } else {
        // 清空选中的楼层
        this.selectedFloors = []
      }
    }
  },
  methods: {
    modelParameterLoaded(val) {
      this.isModelParameterLoaded = val
    },
    /**
     * @description: 获取已关联楼层
     */
    getReleaseFloor() {
      console.log('获取已关联楼层')
      console.log(this.modelList)
      const subAreaId = this.$parent?.$parent?.extraParams?.subAreaId || this.$parent?.$parent?.selectOptions?.id || this.modelList?.[0]?.subAreaId
      console.log(subAreaId)
      if (subAreaId) {
        this.loading = true
        getBimAssociateFloor({
          modelId: this.modelList[0].id,
          subAreaId
        })
          .then((res) => {
            if (res.code === '0') {
              this.selectedFloors = res.data
            }
          })
          .finally(() => {
            this.loading = false
          })
      }
      console.log('关联数据',this.selectedFloors)
    },
    /**
     * @description:确认关联
     */
    handleOk() {
      const floorIdUuidList = []
      const selectedFloors = this.$refs.ModelDetail?.state?.selectedFloors // 选中的楼层
      const originFloor = this.$refs.ModelDetail.originFloor
      const floors =  this.$refs.ModelDetail?.state?.floors
      const floorsChildren =  floors.children[0].children ||[]
      console.log(selectedFloors, originFloor, 'originFloor', floors,floorsChildren)
      selectedFloors.forEach((item) => {
        let floorName = floorsChildren.find(v=>v.id == item)?.name
        const ele = originFloor.find(v=>v?.name == floorName)
        if(ele) {
          let str = item + '_' + ele.uuid
          floorIdUuidList.push(str)
        }
      })
      this.$emit('confirm', selectedFloors, floorIdUuidList)
    },
    // 确认所选模型构件
    async confirmSelect() {
      try {
        const BimModel = this.$refs.ModelDetail;
        this.loading = true
        // console.log(BimModel)
        // console.log(BimModel.selectJuging)
        if (BimModel && BimModel.selectJuging) {
          const entities = await BimModel.selectJuging(); // 得到构件实体信息列表
          console.log('获取完成', entities)
          this.loading = false;
          this.$emit('confirmSelect',entities || []);
        } else {
          this.loading = false;
        }
      } catch {
      }
    },
  },
}
</script>
<style lang="less" scoped>
.dialogBox {
  max-height: calc(var(--heightVh) * 0.75 - 96px);
  overflow: hidden auto;
  scrollbar-width: thin;
  scrollbar-color: #ccc transparent;
  scrollbar-track-color: #fff;
  -ms-scrollbar-track-color: transparent;
  &::-webkit-scrollbar {
    background: #fff;
    width: 8px;
    height: 8px;
    // display: none;
  }
  &::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    width: 8px;
    border-radius: 8px;
    border: 1px #fff solid;
    background: #ccc;
  }
}
</style>